package com.nkzy.bigeventback.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.page.PageMethod;
import com.nkzy.bigeventback.entity.Article;
import com.nkzy.bigeventback.entity.PageBean;
import com.nkzy.bigeventback.mapper.ArticleMapper;
import com.nkzy.bigeventback.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper am;

    @Override
    public void addArticle(Article article) {
        article.setCreateUser(1);//正确的做法是把1换成获取当前登录用户的id
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        am.insertArticle(article);
    }

    @Override
    public PageBean findArticle(int pageNum, int pageSize, Integer categoryId, String state) {
        PageBean<Article> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Article> articleList = am.selectArticle(1,categoryId,state);
        Page p = (Page) articleList;
        pb.setTotal(  p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
}
